//
//  MTSatusViewModel.swift
//  MTWeiBo
//
//  Created by user on 2017/7/27.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
//  封装微博cell的业务逻辑数据处理，对应的cell视图
class MTSatusViewModel: NSObject {

    var status: MTStatuses? {
        didSet{
            handleMbrankImage(mbrank: status?.user?.mbrank ?? 0)
            handleVerifiedTypeImage(verifiedType: status?.user?.verified_type ?? -1)
            if let currentStatus = status {
                //  处理转发微博内容
                handleRetweetContent(statu: currentStatus)
            }
            handleSource(sourceStr: status?.source ?? "")
        }
    }
    
    //会员等级图片
    var mbrankImage: UIImage?
    //认证类型等级图片
    var verifiedTypeImage: UIImage?
    //转发微博内容
    var retweetContent: String?
    //来源
    var source: String?
    
    //来源处理
    private func handleSource(sourceStr: String) {
        print(sourceStr)
        //sourceStr.substring(with: <#T##Range<String.Index>#>)
    }
    
    //转发微博内容处理
    private func handleRetweetContent(statu: MTStatuses) {
        if let retweetStatus = statu.retweeted_status {
            //  有转发微博内容
            //  获取转发微博用户昵称
            if let screenName = retweetStatus.user?.screen_name, let text = retweetStatus.text {
                //  昵称和转发微博内容一定有值，进行字符串的拼接
                retweetContent = "@\(screenName): \(text)"
            }
        }
    }

    
    //认证类型等级图片处理
    private func handleVerifiedTypeImage(verifiedType: Int) {
        switch verifiedType {
        case 0:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
    }
    
    //会员等级图片处理
    private func handleMbrankImage(mbrank: Int) {
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            mbrankImage = nil
        }
    }
}
